package com.hcw.blog.user.web;

import com.hcw.blog.base.constant.UserConst;
import com.hcw.blog.base.vo.ApiResultVo;
import com.hcw.blog.user.entity.UserPO;
import com.hcw.blog.user.entity.vo.UserVO;
import com.hcw.blog.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping(UserConst.USER_URL_PRO)
public class UserController {

    private UserService userService;

    @Autowired
    private void setService(
            UserService userService
    ){
        this.userService = userService;
    }

    /**
     * 创建用户
     * @param userPO
     * @return
     */
    @PostMapping("single/insert")
    public ApiResultVo<Void> insertUser(@RequestBody @Valid UserPO userPO){
        userService.save(userPO);
        return ApiResultVo.success();
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("single/select")
    public ApiResultVo<UserVO> selectUser(@RequestParam("id") String id){
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userService.getById(id), userVO);
        return ApiResultVo.success(userVO);
    }

}
